﻿Imports System.Threading
Imports System.IO

Public Class ControlWindow


    Private wndPagerDisplay As PagerDisplay

    Private Sub btnExitSystem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnExitSystem.Click
        closePagerDisplay()
        Me.Close()
    End Sub

    Private Sub btnStartDisplay_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStartDisplay.Click
        startPagerDisplay()
    End Sub

    Private Sub btnStopDisplay_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStopDisplay.Click
        closePagerDisplay()
        updatePageList()
    End Sub

    Private Sub closePagerDisplay()
        If Not IsNothing(wndPagerDisplay) Then
            wndPagerDisplay.Close()
            wndPagerDisplay = Nothing
            Me.btnStartDisplay.IsEnabled = True
            Me.btnStopDisplay.IsEnabled = False
        End If
    End Sub

    Private Sub updateVersionLabel()
        Dim lbl As Label = FindName("lblVersion")
        lbl.Content = "Version " & Application.AppVersion
        lbl.Visibility = Visibility.Visible
    End Sub

    Private Sub btnEditConfig_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEditConfig.Click
        Dim cw = New EditConfigWindow()
        cw.ShowDialog()
    End Sub

    Private Sub startPagerDisplay()
        If IsNothing(wndPagerDisplay) Then
            wndPagerDisplay = New PagerDisplay()
            ' Add handler so that the Active Page List can be updated when the Pager Display window loads
            AddHandler wndPagerDisplay.Loaded, AddressOf pagerDisplayLoadedHandler

            ' The Pager Display window can take a 1-2 seconds to load so dispatch that into a background thread
            ' so that our control window remains responsive
            wndPagerDisplay.Dispatcher.BeginInvoke(New Action(AddressOf wndPagerDisplay.Show))

            ' Toggle start/stop buttons
            Me.btnStartDisplay.IsEnabled = False
            Me.btnStopDisplay.IsEnabled = True

            ' Make sure control window maintains focus
            Me.Activate()
        End If
    End Sub

    Private Sub pageListChangeHandler(ByVal sender As PageList, ByVal e As EventArgs)
        Debug.WriteLine("PageList Changed: " & sender.toString)
        Me.Dispatcher.BeginInvoke(New Action(AddressOf updatePageList))
    End Sub

    Private Sub updatePageList()
        Dim lst As ListBox = FindName("lstActivePages")
        lst.Items.Clear()

        If IsNothing(wndPagerDisplay) Then
            lst.Items.Add("<STOPPED>")

        Else
            Dim pageList As PageList = wndPagerDisplay.PageList
            If (pageList.isEmpty) Then
                lst.Items.Add("<EMPTY>")
            Else
                For Each page In pageList.getList
                    lst.Items.Add(page)
                Next
            End If

        End If

    End Sub

    Private Sub ControlWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

#If DEBUG Then
        Dim btn As Button = FindName("btnTest")
        If Not IsNothing(btn) Then
            btn.Visibility = Visibility.Visible
        End If
        updatePageList()
#Else
        ' Automatically launch pager display in release mode only
        startPagerDisplay()
#End If
        updateVersionLabel()

    End Sub

    Private Sub pagerDisplayLoadedHandler(ByVal sender As Object, ByVal e As RoutedEventArgs)
        AddHandler wndPagerDisplay.PageList.Changed,
            AddressOf pageListChangeHandler
        updatePageList()
    End Sub

    Private Sub openFile(ByVal fileName As String)
        Dim proc = New Process

        If File.Exists(fileName) Then
            With proc.StartInfo
                .FileName = fileName
                .UseShellExecute = True
                .CreateNoWindow = True
                .RedirectStandardInput = False
                .RedirectStandardOutput = False
                .RedirectStandardError = False
            End With

            proc.Start()
        Else
            MsgBox("Log File does not yet exist at " & fileName)
        End If


    End Sub

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnTest.Click
        Dim assem As Reflection.Assembly = Reflection.Assembly.GetEntryAssembly()
        Dim assemName As Reflection.AssemblyName = assem.GetName()
        Dim ver As Version = assemName.Version
        MsgBox(String.Format("Application {0}, Version {1}", assemName.Name, ver.ToString()))

    End Sub

    Private Sub btnViewLog_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnViewLog.Click
        openFile(Settings.SettingsManager.getValue(Settings.SettingKey.LOG_FILE))
    End Sub

    Private Sub btnViewPageFile_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnViewPageFile.Click
        openFile(Settings.SettingsManager.getValue(Settings.SettingKey.PAGER_FILE))
    End Sub
End Class
